package jms;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Producer {
	static String desName="topic/textTopic";//"queue/A""topic/textTopic"
	public static void main(String[] args) throws NamingException, JMSException {
		Context context=new InitialContext();
		ConnectionFactory connectionFactory=(ConnectionFactory) context.lookup("ConnectionFactory");
		Destination destination=(Destination) context.lookup(desName);//"topic/.."
		
		Connection connection=connectionFactory.createConnection();
//		Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		//true使用事务，手动用session.commit();发送的多条消息具有事务性
		Session session=connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
		MessageProducer producer=session.createProducer(destination);
		
		TextMessage msg=session.createTextMessage();
		msg.setText("hello,this msg comes from hean");
		producer.send(msg);
		session.commit();
		connection.close();
		System.out.println("msg sended");
	}
}
